package com.xposed.aliproxy.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.xposed.aliproxy.R;
import com.xposed.aliproxy.utils.AlertDialogUtil;
import com.xposed.aliproxy.utils.ParamConfig;
import com.xposed.aliproxy.utils.Utils;
import com.xposed.aliproxy.utils.XposeUtil;

public class SetIPActivity extends BaseActivity {
    private String host, port1, port2;
    private EditText edt_ip, edt_port1, edt_port2, edt_deviceId, edt_imei, edt_mac;

    @Override
    public void loadLayout() {
        setContentView(R.layout.activity_setip);
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setTitleStr("设置");
        this.edt_ip = (EditText) findViewById(R.id.setip_edittext_ip);
        this.edt_port1 = (EditText) findViewById(R.id.setip_edittext_port1);
        this.edt_port2 = (EditText) findViewById(R.id.setip_edittext_port2);
        this.edt_deviceId = (EditText) findViewById(R.id.setip_edittext_deviceid);
        this.edt_imei = (EditText) findViewById(R.id.setip_edittext_imei);
        this.edt_mac = (EditText) findViewById(R.id.setip_edittext_mac);

        final ParamConfig paramConfig = ParamConfig.getInstance(context);
        this.host = paramConfig.getString("host", "");
        this.port1 = paramConfig.getString("port1", "");
        this.port2 = paramConfig.getString("port2", "");
        this.edt_ip.setText(this.host);
        this.edt_port1.setText(this.port1);
        this.edt_port2.setText(this.port2);

        getDeviceInfo();

        findViewById(R.id.setip_button_set).setOnClickListener(new OnClickListener() {
            class C00102 implements OnClickListener {
                C00102() {
                }

                public void onClick(View view) {
                    SetIPActivity.this.edt_ip.setText(SetIPActivity.this.host);
                    SetIPActivity.this.edt_port1.setText(SetIPActivity.this.port1);
                    SetIPActivity.this.edt_port2.setText(SetIPActivity.this.port2);
                }
            }

            @Override
            public void onClick(View view) {
                final String host = "47.52.226.177";
                final String port1 = "13006";
                final String port2 = "13005";

//                final String host = SetIPActivity.this.edt_ip.getText().toString().trim();
//                final String port1 = SetIPActivity.this.edt_port1.getText().toString().trim();
//                final String port2 = SetIPActivity.this.edt_port2.getText().toString().trim();
                final String deviceId = SetIPActivity.this.edt_deviceId.getText().toString().trim();
                final String imei = SetIPActivity.this.edt_imei.getText().toString().trim();
                final String mac = SetIPActivity.this.edt_mac.getText().toString().trim();
                if (Utils.isNull(host)) {
                    Toast.makeText(SetIPActivity.this.context, "请输入服务器IP", Toast.LENGTH_SHORT).show();
                } else if (Utils.isNull(port1)) {
                    Toast.makeText(SetIPActivity.this.context, "请输入服务器端口1", Toast.LENGTH_SHORT).show();
                } else if (Utils.isNull(port2)) {
                    Toast.makeText(SetIPActivity.this.context, "请输入服务器端口2", Toast.LENGTH_SHORT).show();
                } else if (Utils.isNull(deviceId)) {
                    Toast.makeText(SetIPActivity.this.context, "请输入设备ID", Toast.LENGTH_SHORT).show();
                } else if (Utils.isNull(imei)) {
                    Toast.makeText(SetIPActivity.this.context, "请输入IMEI", Toast.LENGTH_SHORT).show();
                } else if (Utils.isNull(mac)) {
                    Toast.makeText(SetIPActivity.this.context, "请输入mac地址", Toast.LENGTH_SHORT).show();
                } else {
                    AlertDialogUtil.showAlertDialog(SetIPActivity.this.context, "参数设置", "\n确定要修改最新的服务器IP和端口吗？\n", "确定", "取消", new OnClickListener() {
                        public void onClick(View view) {
                            paramConfig.putString("host", host);
                            paramConfig.putString("port1", port1);
                            paramConfig.putString("port2", port2);
                            XposeUtil.saveDeviceInfo(deviceId, imei, mac);
                            Log.d("ksfdjkalkfd", deviceId + ", " + imei + ", " + mac);
                            Toast.makeText(SetIPActivity.this.context, "参数修改成功", Toast.LENGTH_SHORT).show();
                        }
                    }, new C00102());
                }
            }
        });
    }

    private void getDeviceInfo() {
        String deviceId = XposeUtil.m2992a("deviceId");
        if (Utils.isNull(deviceId)) {
            deviceId = XposeUtil.getDeviceID(this.context);
        }
        String imei = XposeUtil.m2992a("imei");
        if (Utils.isNull(imei)) {
            imei = XposeUtil.getImei(this.context);
        }
        String mac = XposeUtil.m2992a("mac");
        if (Utils.isNull(mac)) {
            mac = XposeUtil.getMac();
        }
        this.edt_deviceId.setText(deviceId);
        this.edt_imei.setText(imei);
        this.edt_mac.setText(mac);
    }
}
